package com.ruoyi.api.user;

import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysUserNotice;
import com.ruoyi.system.domain.dto.UserNoticeCountDTO;
import com.ruoyi.system.service.ISysUserNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户消息Controller
 * 
 * @author ruoyi
 * @date 2022-07-15
 */
@RestController
@RequestMapping("/api/usernotice")
public class UserNoticeController extends BaseController
{
    @Autowired
    private ISysUserNoticeService sysUserNoticeService;

    /**
     * 查询我的消息列表
     */
    @GetMapping("/list")
    public AjaxResult list(SysUserNotice sysUserNotice)
    {
        AjaxResult ajax = AjaxResult.success();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //未读设置为已读
        sysUserNotice.setCreateUser(user.getUserId());
        sysUserNoticeService.updateIsReadAll(user.getUserId());
        startPage();
        List<SysUserNotice> list = sysUserNoticeService.selectSysUserNoticeList(sysUserNotice);
        ajax.put("data",getDataTable(list));
        return ajax;
    }

    /**
     * 我的未读数量
     */
    @GetMapping("/unReadCount")
    public AjaxResult unReadCount(SysUserNotice sysUserNotice)
    {
        AjaxResult ajax = AjaxResult.success();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<UserNoticeCountDTO>  list = sysUserNoticeService.selectUnreadCountByUserId(user.getUserId());
        ajax.put("data",list.get(0));
        return ajax;
    }

    /**
     * 我的未读推送--个推使用
     */
    @GetMapping("/unReadCountInfo")
    public void unReadCountInfo()
    {
        AjaxResult ajax = AjaxResult.success();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<UserNoticeCountDTO>  list = sysUserNoticeService.selectUnreadCountByUserId(user.getUserId());
        if(list!=null && list.size()>0) {
            UserNoticeCountDTO userNoticeCountDTO = list.get(0);
            if(userNoticeCountDTO.getUnreadCount()>0 && StringUtils.isNotEmpty(user.getCid())){
                //根据cid进行单推
                PushDTO<Audience> pushDTO = new PushDTO<Audience>();
                // 设置推送参数
                pushDTO.setRequestId(System.currentTimeMillis() + "");
                /**** 设置个推通道参数 *****/
                PushMessage pushMessage = new PushMessage();
                GTNotification notification = new GTNotification();
                notification.setTitle("未读消息");
                notification.setBody("有未读消息"+userNoticeCountDTO.getUnreadCount()+"条，请留意查看");
                notification.setClickType("payload");
                notification.setPayload("1");
                pushMessage.setNotification(notification);
                pushDTO.setPushMessage(pushMessage);

                /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/
                /*设置接收人信息*/
                Audience audience = new Audience();
                audience.addCid(user.getCid());
                pushDTO.setAudience(audience);
                /*设置接收人信息结束*/
                // 进行cid单推
                PushApi pushApi=GetuiUtils.getPushApi();
                ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
                if (apiResult.isSuccess()) {
                    // success
                    System.out.println(apiResult.getData());
                } else {
                    // failed
                    System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
                }
            }

        }
    }
}
